package com.qf.bigdata.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qf.bigdata.bean.RawSQLResult;
import com.qf.bigdata.service.UserLabelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(path="/api/v1/")
public class UserLabelCtl {
    @Autowired
    @Qualifier("userLabelService")
    private UserLabelService userLabelService;

    @RequestMapping(path="/user/{uid}/label")
    @ResponseBody
    public RawSQLResult queryUserLabel(@PathVariable String uid){
        return userLabelService.queryLabelByUid(uid);
    }
    //http://localhost:7088/api/v1/label/users

    /*
    '{
            "and": [
                    [
                    "gender",
                    "女"
                    ],
                    [
                    "email_suffix",
                    "139.net"
                    ]
            ],
            "or": [
                    [
                    "model",
                    "小米4"
                    ]
            ],
            "op": "and"
        }
     */
    @ResponseBody
    @RequestMapping(path="/label/users",method = RequestMethod.GET,produces ="application/json;charset=UTF-8" )
    public Object queryUsers(@RequestBody JSONObject jsonObject){
        //[["gender","女"],["email_suffix","139.net"]]]
        String andStr= JSONObject.toJSONString(jsonObject.getJSONArray("and"))  ;
        //[["model","小米4"]]
        String orStr=JSONObject.toJSONString(jsonObject.getJSONArray("or"))  ;
        String op=jsonObject.getString("op");
//        return andStr;
        return userLabelService.queryUsersByLabel(
                JSONObject.parseArray(andStr, List.class),
                JSONObject.parseArray(orStr, List.class),op);

    }
}
